// Package _7__礼物的最大价值 main
// @Title _7__礼物的最大价值
// @Description
// @Author  == 琳琳也叫王木木 ==  2022-02-15 9:48
// @Create  2022-02-15 9:48
package main

// 暴力搜索是肯定可以的

func max(a, b int) int {
	if a > b {
		return a
	}
	return b
}

// 动态规划，dp[i][j]表示到i,j 位置的最大价值
func maxValue(grid [][]int) int {
	if len(grid) == 0 || len(grid[0]) == 0 {
		return 0
	}

	m, n := len(grid), len(grid[0])
	for i := 1; i < m; i++ {
		grid[i][0] += grid[i-1][0]
	}
	for j := 1; j < n; j++ {
		grid[0][j] += grid[0][j-1]
	}

	for i := 1; i < m; i++ {
		for j := 1; j < n; j++ {
			grid[i][j] += max(grid[i-1][j], grid[i][j-1])
		}
	}

	return grid[m-1][n-1]
}

func main() {
	nums := [][]int{
		{1, 3, 1},
		{1, 5, 1},
		{4, 2, 1},
	}
	maxValue(nums)
}
